package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GridOrderAdapter;
import com.zhongxi.guoyuan.adapter.RewardOrderAdapter;
import com.zhongxi.guoyuan.entity.rewardcoin.Complaints;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.entity.user.UserData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/3/29.
 */

public class SellerRewardCoinAdapter extends BaseAdapter {

    private Context context;
    private List<RewardCoinOrderResult.RewardCoinOrder> list;
    private Handler handler;
    private int mExpandedMenuPos = -1;
    private int status;
    private JSONObject complaints;
    private JSONObject userinfo;

    public SellerRewardCoinAdapter(Context context, List<RewardCoinOrderResult.RewardCoinOrder> data, Handler handler) {
        this.context = context;
        this.list = data;
        this.handler = handler;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos) {
        this.mExpandedMenuPos = mExpandedMenuPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_seller_reward_order, parent, false);
                holder.relaGet = (RelativeLayout) convertView.findViewById(R.id.relaGet);
                holder.linearPost = (LinearLayout) convertView.findViewById(R.id.linearPost);
                holder.linearComplain = (LinearLayout) convertView.findViewById(R.id.linearComplain);
                holder.ivSingleImg = (View) convertView.findViewById(R.id.ivSingleImg);
                holder.gvImg = (View) convertView.findViewById(R.id.gvImg);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                holder.lvList = (NoScrollListView) convertView.findViewById(R.id.lvList);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
                holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvOper = (TextView) convertView.findViewById(R.id.tvOper);
                holder.tvSend = (TextView) convertView.findViewById(R.id.tvSend);
                holder.ivDown = (ImageView) convertView.findViewById(R.id.iv_down);
                holder.tvOrder = (TextView) convertView.findViewById(R.id.tvOrder);
                holder.ivCall = (ImageView) convertView.findViewById(R.id.ivCall);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvComplain = (TextView) convertView.findViewById(R.id.tvComplain);
                holder.tvAllPrice = (TextView) convertView.findViewById(R.id.tvAllPrice);
                holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost);
                holder.tvReward = (TextView) convertView.findViewById(R.id.tvReward);
                holder.ivTel = (ImageView) convertView.findViewById(R.id.ivTel);
                holder.tvTel = (TextView) convertView.findViewById(R.id.tvTel);
                holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
                holder.iv_phone = (ImageView) convertView.findViewById(R.id.iv_phone);
                holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
                holder.tvPeopleName = (TextView) convertView.findViewById(R.id.tvPeopleName);
                holder.listMenu = (View) convertView.findViewById(R.id.listMenu);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            switch (status){
                case 0:
                    holder.tvOper.setText("叫快递");
                    holder.tvSend.setVisibility(View.VISIBLE);
                    holder.tvSend.setText("发快递");
                    holder.linearComplain.setVisibility(View.GONE);
                    holder.relaGet.setVisibility(View.GONE);
                    holder.linearPost.setVisibility(View.VISIBLE);
                    //叫快递
                    holder.tvOper.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Message msg = Message.obtain();
                            msg.what = 0;
                            msg.obj = position;
                            handler.sendMessage(msg);
                        }
                    });
                    //发快递
                    holder.tvSend.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Message msg = Message.obtain();
                            msg.what = 2;
                            msg.obj = position;
                            handler.sendMessage(msg);
                        }
                    });
                    break;
                case 1:
                    holder.tvOper.setText("被投诉");
                    holder.tvSend.setVisibility(View.GONE);
                    holder.linearComplain.setVisibility(View.VISIBLE);
                    holder.relaGet.setVisibility(View.GONE);
                    holder.linearPost.setVisibility(View.GONE);
                    break;
                case 2:
                    holder.tvOper.setText("确认提货");
                    holder.linearComplain.setVisibility(View.GONE);
                    holder.tvSend.setVisibility(View.GONE);
                    holder.relaGet.setVisibility(View.VISIBLE);
                    holder.linearPost.setVisibility(View.GONE);
                    //确认收货
                    holder.tvOper.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Message msg = Message.obtain();
                            msg.what = 1;
                            msg.obj = position;
                            handler.sendMessage(msg);
                        }
                    });
                    break;
            }

            if(null!=list.get(position)&&!"".equals(list.get(position))){
                RewardCoinOrderResult.RewardCoinOrder data = list.get(position);
                if(data.getImg().size()>0){
                    if(data.getImg().size()==1){
                        holder.ivSingleImg.setVisibility(View.VISIBLE);
                        holder.gvImg.setVisibility(View.GONE);
                        Glide.with(context).load(Url.IMG_URL+data.getImg().get(0)).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
                    }else{
                        holder.ivSingleImg.setVisibility(View.GONE);
                        holder.gvImg.setVisibility(View.VISIBLE);
                        holder.gvList.setAdapter(new GridOrderAdapter(context, data.getImg()));
                    }
                }
                if(data.getGoodsId().size()>0){
                    holder.lvList.setAdapter(new RewardOrderAdapter(context, data, position));
                }
                if(status == 0||status == 1) {
                    if (null != data.getType() && !"".equals(data.getType())) {
                        if ("1".equals(data.getType())) {
                            holder.tvType.setText("取货方式：自提");
                        } else {
                            holder.tvType.setText("取货方式：邮寄");
                        }
                    }
                }else{
                    holder.tvType.setText("取货人：**");
                    holder.tv_phone.setText("取货人电话：**");
                }
                if(null!=data.getOrderid()&&!"".equals(data.getOrderid())){
                    holder.tvOrder.setText("订单号："+data.getOrderid());
                }else{
                    holder.tvOrder.setText("订单号：**");
                }
                if(null!=data.getOrderInfo()&&!"".equals(data.getOrderInfo())) {
                    RewardCoinOrderResult.OrderInfo orderInfo = data.getOrderInfo();
                    switch (status){
                        case 0:
                            if (null != orderInfo.getCtime() && !"".equals(orderInfo.getCtime())) {
                                holder.tvTime.setText("下单时间：" + orderInfo.getCtime());
                            } else {
                                holder.tvTime.setText("下单时间：**");
                            }
                            if("2".equals(data.getType())) {
                                if (null != orderInfo.getConsignee() && !"".equals(orderInfo.getConsignee())) {
                                    holder.tvUserName.setText(orderInfo.getConsignee());
                                } else {
                                    holder.tvUserName.setText("**");
                                }
                                if (null != orderInfo.getConsigneemphone() && !"".equals(orderInfo.getConsigneemphone())) {
                                    holder.tvTel.setText(orderInfo.getConsigneemphone());
                                } else {
                                    holder.tvTel.setText("**");
                                }
                                if (null != orderInfo.getShippingaddress() && !"".equals(orderInfo.getShippingaddress())) {
                                    holder.tvAddress.setText(orderInfo.getShippingaddress());
                                } else {
                                    holder.tvAddress.setText("**");
                                }
                            }
                            break;
                        case 1:
                            if (null != orderInfo.getCtime() && !"".equals(orderInfo.getCtime())) {
                                holder.tvTime.setText("下单时间：" + orderInfo.getCtime());
                            } else {
                                holder.tvTime.setText("下单时间：**");
                            }
                            if(null!=complaints&&!"".equals(complaints)){
                                try {
                                    Complaints complaint = Complaints.parseMatch(complaints.getJSONObject(data.getOrderid()));
                                    UserData userData = UserData.parseMatch(userinfo.getJSONObject(complaint.getUserid()));
                                    if (null != userData.getUsername() && !"".equals(userData.getUsername())) {
                                        holder.tvName.setText(userData.getUsername());
                                        holder.tvType.setText("取货人：" + userData.getUsername());
                                    } else {
                                        holder.tvName.setText("**");
                                        holder.tvType.setText("取货人：**");
                                    }
                                    if (null != userData.getMphone() && !"".equals(userData.getMphone())) {
                                        holder.tvPhone.setText(userData.getMphone());
                                    } else {
                                        holder.tvPhone.setText("**");
                                    }
                                    if (null != complaint.getContent() && !"".equals(complaint.getContent())) {
                                        holder.tvComplain.setText(complaint.getContent());
                                    } else {
                                        holder.tvComplain.setText("**");
                                    }
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                            break;
                        case 2:
                            try {
                                if (null != userinfo && !"".equals(userinfo)) {
                                    UserData userData = UserData.parseMatch(userinfo.getJSONObject(data.getOrderInfo().getUserid()));
                                    if (null != userData.getUsername() && !"".equals(userData.getUsername())) {
                                        holder.tvType.setText("取货人：" + userData.getUsername());
                                        holder.tvPeopleName.setText(userData.getUsername());
                                    } else {
                                        holder.tvType.setText("取货人：**");
                                        holder.tvPeopleName.setText("**");
                                    }
                                    if (null != userData.getMphone() && !"".equals(userData.getMphone())) {
                                        holder.tv_phone.setText(userData.getMphone());
                                        holder.tvTime.setText("取货人电话：" + userData.getMphone());
                                    } else {
                                        holder.tv_phone.setText("**");
                                        holder.tvTime.setText("取货人电话：**");
                                    }
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            break;
                    }
                    if (null != orderInfo.getGoodsnum() && !"".equals(orderInfo.getGoodsnum())) {
                        holder.tvNum.setText("共计" + orderInfo.getGoodsnum() + "件");
                    } else {
                        holder.tvNum.setText("共计**件");
                    }
                    if(null!=orderInfo.getShouldprice()&&!"".equals(orderInfo.getShouldprice())){
                        BigDecimal price = new BigDecimal(orderInfo.getShouldprice());
                        String postage = "0";
                        if(null!=orderInfo.getPostage()&&!"".equals(orderInfo.getPostage())){
                            postage = orderInfo.getPostage();
                        }
                        BigDecimal totalFee = price.add(new BigDecimal(postage));
                        holder.tvMoney.setText("合计金额："+totalFee.toString()+"元[返银币"+totalFee.intValue()+"银币]");
                        holder.tvAllPrice.setText("商品合计："+totalFee.toString()+"元");
                        holder.tvPost.setText("快递："+postage+"元");
                        holder.tvReward.setText("返银："+totalFee.intValue());
                    }else{
                        holder.tvMoney.setText("合计金额：**元[返银币**银币]");
                        holder.tvAllPrice.setText("商品合计：**");
                        holder.tvPost.setText("快递：**元");
                        holder.tvReward.setText("返银：**");
                    }
                }
            }
            holder.ivDown.setImageResource(position == mExpandedMenuPos ? R.mipmap.iv_down_light : R.mipmap.iv_down);
            holder.listMenu.setVisibility(position == mExpandedMenuPos ? View.VISIBLE : View.GONE);

            final String phone = holder.tvPhone.getText().toString().trim();
            final String tel = holder.tvTel.getText().toString().trim();
            final String myPhone = holder.tv_phone.getText().toString().trim();
            //被投诉页面
            holder.ivCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    context.startActivity(intent);
                }
            });
            //待邮寄页面
            holder.ivTel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + tel);
                    intent.setData(data);
                    context.startActivity(intent);
                }
            });
            //待自提页面
            holder.iv_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + myPhone);
                    intent.setData(data);
                    context.startActivity(intent);
                }
            });

        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public void setInfo(JSONObject complaints, JSONObject userinfo) {
        this.complaints = complaints;
        this.userinfo = userinfo;
    }

    public void setStatus(int status) {
        this.status = status;
    }


    public void setInfo(JSONObject userinfo) {
        this.userinfo = userinfo;
    }

    class ViewHolder{
        private RelativeLayout relaGet;
        private LinearLayout linearPost;
        private LinearLayout linearComplain;
        private View ivSingleImg;
        private View gvImg;
        private ImageView ivImg;
        private NoScrollGridView gvList;
        private NoScrollListView lvList;
        private TextView tvNum;
        private TextView tvMoney;
        private TextView tvType;
        private TextView tvTime;
        private TextView tvOper;
        private TextView tvSend;
        private ImageView ivDown;
        private TextView tvOrder;
        private ImageView ivCall;
        private TextView tvPhone;
        private TextView tvName;
        private TextView tvComplain;
        private TextView tvAllPrice;
        private TextView tvPost;
        private TextView tvReward;
        private ImageView ivTel;
        private TextView tvTel;
        private TextView tvUserName;
        private TextView tvAddress;
        private ImageView iv_phone;
        private TextView tv_phone;
        private TextView tvPeopleName;
        private View listMenu;
    }
}
